#!/usr/bin/env python
import sys, os
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-t", "--treefile", dest="treefile",
                    help="Input tree file name")
parser.add_option("-s", "--seq-file", dest="seqfile",
                    help="Input alignmentr sequence file name (FASTA)");
parser.add_option("-o", "--pickle-dump", dest="pickledump",
                    help="Dump the phylogenetic tree into pickle file")
parser.add_option("-i", "--pickle-load", dest="pickleload",
                    help="Load the phylogenetic tree from pickle file")
parser.add_option("--command-file", dest="comfile",
                    help="Command file contains tree editing info.")
parser.add_option("-x", "--extract-nexus", dest="extract_nexus",
                    help="Output nexus as a subtree into a newick formatted file")
parser.add_option("-f", "--fasta-export", dest="fasta_export",
                    help="Export seqeunce pattern")

parser.add_option("--print-nexi", dest='print_nexi', default=False, 
                    action="store_true",
                    help='Print the list of tree nexi with some info.')
parser.add_option("--nexus-paths", dest='nexus_paths', default=False, 
                    action="store_true",
                    help='Show path to nexus for all nexi')

parser.add_option("-n", "--nexus", dest='nexus', default=0, type=int,
                    help="Nexus number selection.")
parser.add_option("--nexus-path", dest='nexus_path', default=False, 
                    action="store_true",
                    help='Show path to nexus')
parser.add_option("--nexus-weblogo", dest='nexus_weblogo', default=False, 
                    action="store_true",
                    help='Output the weblogo of selected nexus.')
parser.add_option("--nexus-species", dest='nexus_species', default=False, 
                    action="store_true",
                    help='Report the species from both clades');
parser.add_option("--nexus-lineage", dest='nexus_lineage', default=False, 
                    action="store_true",
                    help='Detailed info on species lineage')
parser.add_option("--nexus-unique-lineages", dest='nexus_unique_lineages', default=False, 
                    action="store_true",
                    help='Unique lineages found within nexus');
parser.add_option("--nexus-write", dest='nexus_write', default=False, 
                    action="store_true",
                    help='Output the nexus alignment')
parser.add_option("--draw-ascii", dest='draw_ascii', default=False, 
                    action="store_true",
                    help='Draw the ascii version of nexus')
parser.add_option("--print-info", dest='print_info', default=False, 
                    action="store_true",
                    help='Print the tree information.')
parser.add_option("--nexus-geni", dest='nexus_geni', default=False, 
                    action="store_true",
                    help='Report genuses from both clades');
parser.add_option("--nexus-aa", dest='nexus_aa', 
					help='Show stretch of the sequence for every protein of the nexus.')
parser.add_option("--nexi-with-genus", dest='nexi_with_genus', 
					help='Show the list of nexi that contain proteins form the specified genus.')

parser.add_option("--nexus-cutoff", dest='nexus_cutoff', default=1, type=int,
					help="Nexus clade size cutoff.")
parser.add_option("--email", dest='email', default="anonymous@phylitree",
					help="Email address for NCBI access.")
parser.add_option("-v", action="count", dest="verbose", default=0,
                    help='verbose output')
parser.add_option("--no-degap", dest='degap', default=True, 
                    action="store_false",
                    help='Degap output sequences.')
parser.add_option("--non-ladderized", dest='ladderize', default=True, 
                    action="store_false",
                    help='Do not ladderize phylogenetic trees')
parser.add_option("--no-clades", dest='no_clades', default=False, 
                    action="store_true",
                    help='Do not output nexus clades when writing alignments/weblogos')
parser.add_option("--lineage-cols", dest='lineage_cols', default='superkingdom,phylum,order,genus,species',
					help='Comma-separated list of columns for lineage tables.  Permitted values are class,family,genus,infraclass,infraorder,kingdom,order,phylum,species,species group,subclass,subfamily,subgenus,suborder,subphylum,subspecies,superclass,superfamily,superkingdom,superphylum,tribe.')
parser.add_option("--imgtype", dest='imgtype', default='pdf',
                    help='Weblogo output format.  Permitted values are png, eps, jpg, pdf (default), pngprint, txt.')
parser.add_option("--weblogo-ticks", dest='annotate',
					help='Weblogo tick labels.  Same length as sequence, comma-separated.')
parser.add_option("--weblogo-number-fontsize", dest="weblogo_number_fontsize", default=8, type=int,
					help="Font size for weblogo tick labels, in points.")


(options, args) = parser.parse_args()

from Bio import Entrez
Entrez.email = options.email


import cPickle, tempfile
from helper import *

if options.pickleload:
	with open(options.pickleload) as f:
		tree = cPickle.load(f)
else:
	tree = PhylipTree(options.treefile, options.seqfile, options.ladderize)

tree.set_nexus_cutoff(options.nexus_cutoff)

if options.comfile:
	tree.edit(options.comfile)

if options.print_nexi:
	tree.nexus_report(options.verbose)
if options.print_info:
	tree.info_report()
if options.nexus_weblogo:
	tree.nexus_weblogo(options)
if options.nexus_write:
	tree.nexus_write(options)
if options.nexus_species:
	tree.nexus_species(options)
if options.nexus_geni:
	tree.nexus_geni(options)
if options.draw_ascii:
	tree.nexus_draw(tree.get_nexus(options.nexus))
if options.pickledump:
	tree.pickle_dump(options.pickledump)
if options.nexus_path:
	tree.print_nexus_path(options.nexus)
if options.nexus_paths:
	tree.print_nexus_paths()
if options.nexi_with_genus:
	tree.print_nexi_with_genus(options)
if options.nexus_aa:
	tree.print_nexus_aa(options)
if options.nexus_lineage:
	tree.print_nexus_lineage(options)
if options.nexus_unique_lineages:
	tree.print_nexus_unique_lineages(options)
if options.extract_nexus:
	tree.extract_nexus(options)
if options.fasta_export:
	tree.export_sequence_pattern(options)
